package com.wonders.module.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.common.config.manager.instance.DicManager;
import com.wonders.common.constant.TipConstant;
import com.wonders.framework.util.web.RequestUtil;
import com.wonders.framework.web.BaseController;
import com.wonders.module.base.entity.vo.DicVo;

/**
 * 数据字典控制层
 * 
 * @author WuBin
 * 
 */
@Component
@RequestMapping("/base/dic")
public class DicController extends BaseController {

	@Autowired
	private DicManager dicManager;

	/**
	 * 获取一项字典数据
	 * 
	 * @return
	 */
	@RequestMapping("getSingleDic")
	@ResponseBody
	public Object getSingleDic() {
		String dicType = (String) RequestUtil.getRequestParam("dicType");
		String parentCode = (String) RequestUtil.getRequestParam("parentCode");
		if (StringUtils.isEmpty(dicType)) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		return dicManager.getDicList(dicType, parentCode);
	}

	/**
	 * 获取多项字典数据
	 * 
	 * @return
	 */
	@RequestMapping("getMutiDic")
	@ResponseBody
	public Object getMutiDic() {
		String dicTypes = (String) RequestUtil.getRequestParam("dicType");
		if (StringUtils.isEmpty(dicTypes)) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		Map<String, List<DicVo>> dicMap = new HashMap<String, List<DicVo>>();
		for (String dicType : dicTypes.split(",")) {
			dicMap.put(dicType, dicManager.getDicList(dicType));
		}
		return dicMap;
	}

}
